package com.zhentao.article.kafka.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhentao.mapper.AparticleConfigMapper;
import com.zhentao.model.article.pojo.ApArticleConfig;
import org.apache.commons.lang.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ArticleDownOrUpListener {
    @Autowired
    private AparticleConfigMapper aparticleConfigMapper;

    @KafkaListener(topics = "articleDownOrUpTopic")
    public void ArticleDownOrUp(String message) {
        System.out.println("获取到kafka的数据"+message);
        if (StringUtils.isNotBlank(message)){
            Map map = JSON.parseObject(message, Map.class);
//            修改文章的状态
            QueryWrapper<ApArticleConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(ApArticleConfig::getArticleId,map.get("articleId"));
            ApArticleConfig apArticleConfig = aparticleConfigMapper.selectOne(queryWrapper);

            int getEnable = (int) map.get("enable");
            boolean enable = true;
            if (getEnable == 1){
                enable = false;
            }

            apArticleConfig.setIsDown(enable);
            aparticleConfigMapper.updateById(apArticleConfig);
        }
    }
}
